var member;
$(function(){
	member=getEntity("memberInfo");
	init();
})


/**默认地址*****/
function setDefaultAddress(maId){
	$.ajax({
		type:'post',
		url:rootPath+'/address/setDefaultAddress',
		data:{'maId':maId},
		success:function(data){
			if(data.resultDesc){
				init();
			}
		}
	})
}



function moAdress(maId){
	window.location.href=rootPath+"/address/updateAddress?maId="+maId;
}
function init(){
	$.ajax({
		type:'post',
		url:rootPath+'/address/selectBymId',
		data:{'mId':member.mId},
		success:function(data){
			if(data.resultDesc){
				var addressList=data.data;
				//console.log(addressList);
				var str='';
				str=createHtml(addressList);
				//console.log(str);
				$("#mAdress").empty();
				$("#mAdress").append(str);
			}
		}
	})
}
function createHtml(addressList){
	var str='';
	var imgDefault='';
	var isDefault='';
	var ma_id = $("#ma_id").val();
	
	
	$.each(addressList,function(i,item){
		if(item.isdefault=="0"){
			imgDefault=rootPath+'/image/agree1.png';
			isDefault='';
		}else{
			imgDefault=rootPath+'/image/agree.png';
			isDefault='<span style="color:red" class="fl">【默认地址】</span>';
		}
		str+='<div class=" clearfix selectAdresOrder maAdress"> '
		//	+' <ul class="maAdd" onclick="btnAddress('\"+item.maId+"\')"><li> '
			+" <ul class='maAdd' onclick='btnAddress"+'("'+item.maId+'");'+"'><li> "
			
			+'<p>'+item.name+'</p><span class="postcode">'+item.postcode+'</span><span class="fr">'+item.tel+'</span></li>'
			+'<li class="wang"> '
			if(ma_id!=null && ma_id!='' && ma_id==item.maId){
				str+='<p style="color:red">'+isDefault+''+item.streetAll+'</p> '
			}else{
				str+='<p>'+isDefault+''+item.streetAll+'</p> '
			}
			str+=' </li></ul>'
	//		+'<div class="selectA"><img onclick="setDefaultAddress(\''+item.maId+'\')" src="'+imgDefault+'" alt="" class="fl selectImg"/><span class="fl">默认地址</span>';
	//		+'<a href="#" onclick="deleteAddress(\''+item.maId+'\')"><span class="fr selectM">删除</span><img src="'+rootPath+'/image/dl1.png" alt="" class="fr"/></a>'
	//		+'<a href="javascript:void(0);"  onclick="moAdress(\''+item.maId+'\');"><span class="fr" >修改</span><img src="'+rootPath+'/image/mod.png" alt="" class="fr"/></a></div></div>';
	});
	return str;
}

/**优化删除，删除时给入提示：是否确定删除，删除后需要重新添加*/
function deleteAddress(maId){
	if(confirm("确定要删除地址吗？")){
		$.ajax({
			type:'post',
			url:rootPath+'/address/deleteAddressById',
			data:{'maId':maId},
			success:function(data){
				if(data.resultDesc){
					location.reload(true);
				}
			}
		});
	}else{
		return false;
	}
}

/***选中后事件******/
function btnAddress(maId){
	window.location.href = rootPath+"/order/order?maId="+maId;
}
